<?php
namespace Admin\Controller;

class MemberController extends AdminController {

    public function index(){
    	$username = I('username');
		$obj = M('Member');	
		if ($username != '') {
			$map['username'] = array('like','%'.$username.'%');
		}
		$map['userType'] = array('gt',0);
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('userid desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('username', $username);
		$this->display();
    }

    public function user(){
    	$username = I('username');
		$obj = M('Member');	
		if ($username != '') {
			$map['username'] = array('like','%'.$username.'%');
		}
		$map['userType'] = 0;
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('userid desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('username', $username);
		$this->display();
    }


    public function add(){
    	$this->assign('password',createNonceStr(8));
    	$this->display();
    }

    public function save(){
    	$obj = D('Member');
        if ($data = $obj->create()) {
    		if ($userID = $obj->add($data)) {
	        	//发送邮件
	        	$name = '';
                $title = C('site_name').' member email';
                $this->assign('username', $data['username']);
                $this->assign('password', I('post.password'));
                $content = $this->fetch('email');
                $url = 'http://api.kf01.com/email/mentoring.php';
                $emailDate = array(
                        'email' => $data['username'],
                        'username' => $name,
                        'title' => $title,
                        'content' => $content,
                    );
                $this->https_post($url,$emailDate);

                $state = 'SUCCESS';
	            $message = L('_action_success');
	            $url = U('Member/index');
            } else {
                $state = L('_action_fail');
            }        	          
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }


	#编辑
	public function edit(){
		$userid = (int) $_GET['userid'];
		if (!isset ($userid)) {
			$this->error('Parameter error');
		}
		$obj = M('Member');
		$list = $obj->where("userid=$userid")->find();
		if (!$list) {
			$this->error('Information does not exist');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}

	

	#更新
	public function update() {
		$obj = D('Member');
        if ($data = $obj->create()) {

        	if ($data['password']=='') {
        		unset($data['password']);
        	}else{
        		$data['password'] = md5($data['password']);
        	}


            if ($obj->save($data)) {
                $state = 'SUCCESS';
                $message = 'Successful operation';
                $url = U('Member/index');
            } else {
                $state = 'Operation failed';
            }
        }else{
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
	}



	#删除
	public function del(){
		$userid=I('post.selectedids');
		if($userid==''){
			$state = "You do not select any information!";
		}else{
			foreach ($userid as $v) {
				$obj = M('Member');
				$where['userid'] = $v;
				$user=$obj->where($where)->find();
				if($user['userType']==1){
					unset($map);
					$map['userid']=$v;
					M("Mentees")->where($map)->setField("del",1);
				}else{
					unset($map);
					$map['userid']=$v;
					M("Mentors")->where($map)->setField("del",1);
				}
				$obj->where($where)->delete();
			}
			$state = 'SUCCESS';
	        $message = 'Successful operation';
	        $url = U('Member/index');
		}
		echo $this->echo_json_str($state,$message,$url);
	}

	#编辑
	public function move(){
		$userid = (int) $_GET['userid'];
		if (!isset ($userid)) {
			$this->error('Parameter error');
		}
		$obj = M('Member');
		$list = $obj->where("userid=$userid")->find();
		if (!$list) {
			$this->error('Information does not exist');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}

	#编辑
	public function movesave(){
		$obj = M('Member');
        if ($data = $obj->create()) {
        	if ($data['userType']=='') {
        		echo $this->echo_json_str('Please select user identity',$message,$url);
        		die;
        	}
            if ($obj->save($data)) {
                $state = 'SUCCESS';
                $message = 'Successful operation';
                $url = U('Member/index');    

                //发送邮件
	        	$name = '';
                $title = C('site_name').' member email';
                $content = $this->fetch('movemail');
                $url = 'http://api.kf01.com/email/mentoring.php';
                $emailDate = array(
                        'email' => I('post.sendemail'),
                        'username' => $name,
                        'title' => $title,
                        'content' => $content,
                    );
                $this->https_post($url,$emailDate);                           
            } else {
                $state = 'Operation failed';
            }
        }else{
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
	}

	public function detail(){
		$userid = ( int ) $_GET ['userid'];		
		if (! isset ( $userid )) {			
			$this->error ( L ( "_parm_error" ) );
		}		
		$obj = M ( 'User' );		
		$list = $obj->where ( "userid=$userid" )->find ();
		$this->assign('list',$list);
		$this->display ();
	}
}